using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SpaceShooter.Utilities;

namespace SpaceShooter.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CollisionDetectorComponent : Microsoft.Xna.Framework.GameComponent
    {
        StateManagement.GameState gs;
        PlayerComponent player;

        public CollisionDetectorComponent(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            gs = (StateManagement.GameState)Game.Services.GetService(typeof(StateManagement.GameState));
            player = (PlayerComponent)Game.Services.GetService(typeof(PlayerComponent));
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!gs.pause)
            {
                if (!gs.timeTraveling)
                {
                    foreach (AsteroidsManagerComponent.Asteroid x in gs.asteroids)
                    {
                        if (x.lifeState != LifeState.DEAD)
                        {
                            if (Collision(x.position, gs.playerPosition))
                            {
                                int temp = x.life;
                                x.Damage(gs.playerLife, x.position - gs.playerPosition);
                                player.Damage(temp);
                            }
                        }
                    }
                    foreach (PowerUpManager.PowerUp x in gs.powerUps)
                    {
                        if (x.lifeState != LifeState.DEAD)
                        {
                            if (Collision(x.Position, gs.playerPosition))
                            {
                                player.Upgrade(x.Type);
                                x.lifeState = LifeState.DEAD;
                            }
                        }
                    }
                    foreach (EnemyManagerComponent.Enemy x in gs.enemies)
                    {
                        if (x.LifeState != LifeState.DEAD)
                        {
                            if (Collision(x.Position, gs.playerPosition))
                            {
                                x.Damage(20);
                                player.Damage(20);
                            }
                            foreach (AsteroidsManagerComponent.Asteroid y in gs.asteroids)
                            {
                                if (y.lifeState != LifeState.DEAD)
                                {
                                    if (Collision(x.Position, y.position))
                                    {
                                        int temp = x.Life;
                                        x.Damage(y.life);
                                        y.Damage(temp, x.Position - y.position);
                                    }
                                }
                            }
                        }
                    }
                    foreach (BulletsManagerComponent.Bullet x in gs.playerBullets)
                    {
                        if (x.lifeState != LifeState.DEAD)
                        {
                            foreach (EnemyManagerComponent.Enemy y in gs.enemies)
                            {
                                if (y.LifeState != LifeState.DEAD)
                                {
                                    if (Collision(x.position, y.Position))
                                    {
                                        y.Damage(x.damage);
                                        x.lifeState = LifeState.DEAD;
                                    }
                                }
                            }
                            foreach (AsteroidsManagerComponent.Asteroid y in gs.asteroids)
                            {
                                if (y.lifeState != LifeState.DEAD)
                                {
                                    if (Collision(x.position, y.position))
                                    {
                                        y.Damage(x.damage, x.position - y.position);
                                        x.lifeState = LifeState.DEAD;
                                    }
                                }
                            }
                        }
                    }
                    foreach (BulletsManagerComponent.Bullet x in gs.enemiesBullets)
                    {
                        if (x.lifeState != LifeState.DEAD)
                        {
                            if (Collision(x.position, gs.playerPosition))
                            {
                                player.Damage(x.damage);
                                x.lifeState = LifeState.DEAD;
                            }
                            foreach (AsteroidsManagerComponent.Asteroid y in gs.asteroids)
                            {
                                if (y.lifeState != LifeState.DEAD)
                                {
                                    if (Collision(x.position, y.position))
                                    {
                                        y.Damage(x.damage, x.position - y.position);
                                        x.lifeState = LifeState.DEAD;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            base.Update(gameTime);
        }

        public bool Collision(Vector2 a, Vector2 b)
        {
            Rectangle A = Utilities.Utility.newRectangleFromCenterPosition(a, 20, 20);
            Rectangle B = Utilities.Utility.newRectangleFromCenterPosition(b, 10, 10);
            if (A.Intersects(B))
                return true;
            else
                return false;
        }
    }
}
